


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html id="htmlId">
<head>
  <title>Coverage Report :: ParameterDiscovererBuilder</title>
  <style type="text/css">
    @import "../../.css/coverage.css";
  </style>
</head>

<body>
<div class="header"></div>

<div class="content">
<div class="breadCrumbs">
    [ <a href="../../index.html">all classes</a> ]
    [ <a href="../index.html">at.schauer.gregor.vienna.serializer.builder</a> ]
</div>

<h1>Coverage Summary for Class: ParameterDiscovererBuilder (at.schauer.gregor.vienna.serializer.builder)</h1>

<table class="coverageStats">
<tr>
  <th class="name">Class</th>
<th class="coverageStat 
">
  Class, %
</th>
<th class="coverageStat 
">
  Method, %
</th>
<th class="coverageStat 
">
  Line, %
</th>
</tr>
<tr>
  <td class="name">ParameterDiscovererBuilder</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (1/ 1)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (5/ 5)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    87.1%
  </span>
  <span class="absValue">
    (27/ 31)
  </span>
</td>
</tr>

</table>

<br/>
<br/>


<div class="sourceCode"><i>1</i>&nbsp;/*
<i>2</i>&nbsp; * Copyright 2012 Gregor Schauer
<i>3</i>&nbsp; *
<i>4</i>&nbsp; * Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<i>5</i>&nbsp; * you may not use this file except in compliance with the License.
<i>6</i>&nbsp; * You may obtain a copy of the License at
<i>7</i>&nbsp; *
<i>8</i>&nbsp; *      http://www.apache.org/licenses/LICENSE-2.0
<i>9</i>&nbsp; *
<i>10</i>&nbsp; * Unless required by applicable law or agreed to in writing, software
<i>11</i>&nbsp; * distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<i>12</i>&nbsp; * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<i>13</i>&nbsp; * See the License for the specific language governing permissions and
<i>14</i>&nbsp; * limitations under the License.
<i>15</i>&nbsp; */
<i>16</i>&nbsp;package at.schauer.gregor.vienna.serializer.builder;
<i>17</i>&nbsp;
<i>18</i>&nbsp;import at.schauer.gregor.vienna.context.ViennaContext;
<i>19</i>&nbsp;import at.schauer.gregor.vienna.serializer.builder.discover.ParameterDiscoverer;
<i>20</i>&nbsp;import at.schauer.gregor.vienna.util.ViennaUtils;
<i>21</i>&nbsp;import org.apache.commons.lang3.ClassUtils;
<i>22</i>&nbsp;import org.springframework.beans.BeanUtils;
<i>23</i>&nbsp;
<i>24</i>&nbsp;import java.lang.reflect.Constructor;
<i>25</i>&nbsp;import java.util.HashMap;
<i>26</i>&nbsp;import java.util.List;
<i>27</i>&nbsp;import java.util.Map;
<i>28</i>&nbsp;
<i>29</i>&nbsp;/**
<i>30</i>&nbsp; * @author Gregor Schauer
<i>31</i>&nbsp; */
<i>32</i>&nbsp;public class ParameterDiscovererBuilder&lt;T&gt; implements ObjectBuilder&lt;T&gt; {
<i>33</i>&nbsp;	protected Map&lt;Class&lt;?&gt;, ParameterDiscoverer&gt; discovererMap;
<i>34</i>&nbsp;	protected ViennaContext ctx;
<i>35</i>&nbsp;
<i>36</i>&nbsp;	public ParameterDiscovererBuilder(ViennaContext ctx) {
<b class="fc"><i>37</i>&nbsp;		this(ctx, new HashMap&lt;Class&lt;?&gt;, ParameterDiscoverer&gt;());</b>
<b class="fc"><i>38</i>&nbsp;	}</b>
<i>39</i>&nbsp;
<b class="fc"><i>40</i>&nbsp;	public ParameterDiscovererBuilder(ViennaContext ctx, Map&lt;Class&lt;?&gt;, ParameterDiscoverer&gt; discovererMap) {</b>
<b class="fc"><i>41</i>&nbsp;		this.ctx = ctx;</b>
<b class="fc"><i>42</i>&nbsp;		this.discovererMap = discovererMap;</b>
<b class="fc"><i>43</i>&nbsp;	}</b>
<i>44</i>&nbsp;
<i>45</i>&nbsp;	@Override
<i>46</i>&nbsp;	public &lt;E extends T&gt; E construct(Map&lt;String, Object&gt; map, Class&lt;E&gt; clazz) {
<b class="fc"><i>47</i>&nbsp;		ParameterDiscoverer discoverer = discovererMap.get(clazz);</b>
<b class="fc"><i>48</i>&nbsp;		if (discoverer == null) {</b>
<b class="fc"><i>49</i>&nbsp;			List&lt;Class&lt;?&gt;&gt; classes = ClassUtils.getAllSuperclasses(clazz);</b>
<b class="fc"><i>50</i>&nbsp;			classes.addAll(ClassUtils.getAllInterfaces(clazz));</b>
<i>51</i>&nbsp;
<b class="fc"><i>52</i>&nbsp;			for (Class&lt;?&gt; type : classes) {</b>
<b class="fc"><i>53</i>&nbsp;				discoverer = discovererMap.get(type);</b>
<b class="fc"><i>54</i>&nbsp;				if (discoverer != null) {</b>
<b class="fc"><i>55</i>&nbsp;					discovererMap.put(clazz, discoverer);</b>
<b class="fc"><i>56</i>&nbsp;					break;</b>
<i>57</i>&nbsp;				}
<i>58</i>&nbsp;			}
<i>59</i>&nbsp;
<b class="fc"><i>60</i>&nbsp;			if (discoverer == null) {</b>
<b class="nc"><i>61</i>&nbsp;				throw new RuntimeException();</b>
<i>62</i>&nbsp;			}
<i>63</i>&nbsp;		}
<i>64</i>&nbsp;
<i>65</i>&nbsp;		try {
<b class="fc"><i>66</i>&nbsp;			Constructor&lt;E&gt; ctor = clazz.getDeclaredConstructor(discoverer.getParameterTypes());</b>
<b class="fc"><i>67</i>&nbsp;			String[] parameterNames = discoverer.getParameterNameDiscoverer().getParameterNames(ctor);</b>
<i>68</i>&nbsp;
<b class="fc"><i>69</i>&nbsp;			Class&lt;?&gt;[] parameterTypes = discoverer.getParameterTypes();</b>
<b class="fc"><i>70</i>&nbsp;			Object[] args = new Object[discoverer.getParameterTypes().length];</b>
<i>71</i>&nbsp;
<b class="fc"><i>72</i>&nbsp;			for (int i = 0; i &lt; parameterTypes.length; i++) {</b>
<b class="fc"><i>73</i>&nbsp;				args[i] = map.get(parameterNames[i]);</b>
<b class="fc"><i>74</i>&nbsp;				if (args[i] instanceof String) {</b>
<b class="nc"><i>75</i>&nbsp;					args[i] = ViennaUtils.DeserializationUtils.load(ctx, parameterTypes[i], (String) args[i]);</b>
<i>76</i>&nbsp;				}
<i>77</i>&nbsp;			}
<b class="fc"><i>78</i>&nbsp;			return BeanUtils.instantiateClass(ctor, args);</b>
<b class="nc"><i>79</i>&nbsp;		} catch (NoSuchMethodException e) {</b>
<b class="nc"><i>80</i>&nbsp;			throw new RuntimeException(e);</b>
<i>81</i>&nbsp;		}
<i>82</i>&nbsp;	}
<i>83</i>&nbsp;
<i>84</i>&nbsp;	public Map&lt;Class&lt;?&gt;, ParameterDiscoverer&gt; getDiscovererMap() {
<b class="fc"><i>85</i>&nbsp;		return discovererMap;</b>
<i>86</i>&nbsp;	}
<i>87</i>&nbsp;
<i>88</i>&nbsp;	public void setDiscovererMap(Map&lt;Class&lt;?&gt;, ParameterDiscoverer&gt; discovererMap) {
<b class="fc"><i>89</i>&nbsp;		this.discovererMap = discovererMap;</b>
<b class="fc"><i>90</i>&nbsp;	}</b>
<i>91</i>&nbsp;}
</div>
</div>

<div class="footer">
    
    <div style="float:right;">generated on 2012-07-17 20:23</div>
</div>
</body>
</html>
